#include <iostream>
#include <unistd.h>
#include <functional>
#include "BlockingQueue.hpp"

void DownLoad()
{
    std::cout << "下载一个任务" << std::endl;
    sleep(3); // 假设任务的耗时。
}

using task_t = std::function<void()>;

void* Producer(void* arg)
{
    BlockingQueue<task_t>* bq = static_cast<BlockingQueue<task_t>*>(arg);
    while(true)
    {
        bq->Push(DownLoad);
        std::cout << "生产了一个任务" << std::endl;
        sleep(3);
    }
}

void* Consumer(void* arg)
{
    BlockingQueue<task_t>* bq = static_cast<BlockingQueue<task_t>*>(arg);
    while(true)
    {
        task_t task = bq->Pop();
        task();
        sleep(3);
    }
}

int main()
{
    BlockingQueue<task_t> bq;

    pthread_t p[2],c[2];

    pthread_create(p,nullptr,Producer,&bq);
    pthread_create(p+1,nullptr,Producer,&bq);
    pthread_create(c,nullptr,Consumer,&bq);
    pthread_create(c+1,nullptr,Consumer,&bq);

    pthread_join(p[0],nullptr);
    pthread_join(p[1],nullptr);
    pthread_join(c[0],nullptr);
    pthread_join(c[1],nullptr);

    return 0;
}